<?php

Yii::import('zii.widgets.CListView');
class BaseDataProviderWidget extends CListView{
    public $isAjaxResponse = false;
    
    public static $isGlobalAjaxRequest = false;
  
    public $scrollTrigger = false;
    
    public $scrollOffset = '100';
    
    public $scrollContainerId = "#scroll-container";
    
    public $crossOver = null;

    public $tagName = 'section';
    
    
    public function init(){
        parent::init();
        Yii::trace('inside '.$this->getId());
        (isset($_GET[$this->ajaxVar]) && $_GET[$this->ajaxVar] == $this->getId()) ? 
                $this->isAjaxResponse = true :
                $this->isAjaxResponse = false;
      //  (isset($_GET[$this->ajaxVar]) && $_GET[$this->ajaxVar] != $this->getId()) ? self::$isGlobalAjaxRequest = true : '';
     
       $this->baseScriptUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('application.components.widgets.js'));

    }

    
    public function run() {
        //ob_end_clean();
        parent::run();
    }

    public function renderShowAll(){
        echo 'show all';
    }
    public function renderContent() {
        if($this->isAjaxResponse === true){
                
            $this->renderAjaxContent();
            Yii::app()->end();                    
        }
        if(self::$isGlobalAjaxRequest !== true){
            parent::renderContent();            
        }
    }
    
    public function renderAjaxContent(){
        ob_clean();
 //       echo 'sent by '.$this->getId().'<br/>';
            parent::renderContent();
        ob_end_flush();
    }


    public function renderPager(){
        
          if(!$this->enablePagination)
                        return;
//          if($this->isAjaxResponse)
//              return;

                $pager=array();
                $class='CustomPager';
                if(is_string($this->pager))
                        $class=$this->pager;
                else if(is_array($this->pager))
                {
                        $pager=$this->pager;
                        if(isset($pager['class']))
                        {
                                $class=$pager['class'];
                                unset($pager['class']);
                        }
                }
                $pager['pages']=$this->dataProvider->getPagination();

                if($pager['pages']->getPageCount()>1)
                {
                        echo '<div class="'.$this->pagerCssClass.'">';
                        
                                $class='CustomPager';
                        $this->widget($class,$pager);
                        echo '</div>';
                }
                else{
                            $class='CustomPager';
                        $this->widget($class,$pager);
                }
    }
    
    public function registerClientScript() {
          $id=$this->getId();

                if($this->ajaxUpdate===false)
                        $ajaxUpdate=array();
                else
                        $ajaxUpdate=array_unique(preg_split('/\s*,\s*/',$this->ajaxUpdate.','.$id,-1,PREG_SPLIT_NO_EMPTY));
                $options=array(
                        'ajaxUpdate'=>$ajaxUpdate,
                        'ajaxVar'=>$this->ajaxVar,
                        'pagerClass'=>$this->pagerCssClass,
                        'loadingClass'=>$this->loadingCssClass,
                        'sorterClass'=>$this->sorterCssClass,
                        'scrollOffset'=>$this->scrollOffset,
                        'scrollContainerId'=>$this->scrollContainerId,
                        'scrollTrigger'=>$this->scrollTrigger,
                        'crossOver'=>$this->crossOver
                );
                if($this->ajaxUrl!==null)
                        $options['url']=CHtml::normalizeUrl($this->ajaxUrl);
                if($this->updateSelector!==null)
                        $options['updateSelector']=$this->updateSelector;
                if($this->beforeAjaxUpdate!==null)
                        $options['beforeAjaxUpdate']=(strpos($this->beforeAjaxUpdate,'js:')!==0 ? 'js:' : '').$this->beforeAjaxUpdate;
                if($this->afterAjaxUpdate!==null)
                        $options['afterAjaxUpdate']=(strpos($this->afterAjaxUpdate,'js:')!==0 ? 'js:' : '').$this->afterAjaxUpdate;

                $options=CJavaScript::encode($options);
                $cs=Yii::app()->getClientScript();
                $cs->registerCoreScript('jquery');
                $cs->registerCoreScript('bbq');
                $cs->registerScriptFile($this->baseScriptUrl.'/jquery.yiilistview.js',CClientScript::POS_END);
             //   $cs->registerScriptFile(Yii::app()->basePath.'/components/widgets/js/jquery.yiilistview.js',CClientScript::POS_END);
                $cs->registerScript(__CLASS__.'#'.$id,"jQuery('#$id').yiiListView($options);");

    }
}